package com.exam.controller;

import com.exam.entity.*;
import com.exam.entity.common.ResponseResult;
import com.exam.service.*;
import com.exam.util.JsonUtils;
import com.exam.util.SimilarityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
public class ExamUserController {

    @Autowired
    @Qualifier("examUserServiceImpl")
    private ExamUserService examUserService;
    ResponseResult responseResult = new ResponseResult();

    @Autowired
    @Qualifier("singleChoiceQServiceImpl")
    private SingleChoiceQService singleChoiceQService;
    @Autowired
    @Qualifier("judgmentQServiceImpl")
    private JudgmentQService judgmentQService;

    @Autowired
    @Qualifier("shortAnswerQServiceImpl")
    private ShortAnswerQService shortAnswerQService;
    @Autowired
    @Qualifier("fillBlanksQServiceImpl")
    private FillBlanksQService fillBlanksQService;

    @RequestMapping("/get_not_finish_exam_by_u_id")
    @ResponseBody
    public String get_not_finish_exam_by_u_id(int u_id) {
        List<ExamUser> notFinishExamByUId = examUserService.get_not_finish_exam_by_u_id(u_id);
        responseResult.setCode(200);
        responseResult.setMessage("成功");
        responseResult.setResult(notFinishExamByUId);
        return JsonUtils.getJson(responseResult);
    }

    @RequestMapping("/get_finish_exam_by_u_id")
    @ResponseBody
    public String get_finish_exam_by_u_id(int u_id) {
        List<ExamUser> finishExamByUId = examUserService.get_finish_exam_by_u_id(u_id);
        responseResult.setCode(200);
        responseResult.setMessage("成功");
        responseResult.setResult(finishExamByUId);
        return JsonUtils.getJson(responseResult);
    }


    @RequestMapping("/get_not_finish_exam_user_by_e_id")
    @ResponseBody
    public String get_not_finish_exam_user_by_e_id(int e_id) {
        List<ExamUser> notFinishExamUserByEId = examUserService.get_not_finish_exam_user_by_e_id(e_id);
        responseResult.setCode(0);
        responseResult.setMessage("成功");
        responseResult.setResult(notFinishExamUserByEId);
        return JsonUtils.getJson(responseResult);
    }

    @RequestMapping("/get_finish_exam_user_by_e_id")
    @ResponseBody
    public String get_finish_exam_user_by_e_id(int e_id) {
        List<ExamUser> notFinishExamUserByEId = examUserService.get_finish_exam_user_by_e_id(e_id);
        responseResult.setCode(0);
        responseResult.setMessage("成功");
        responseResult.setResult(notFinishExamUserByEId);
        return JsonUtils.getJson(responseResult);
    }

    @RequestMapping("/del_exam_user_by_eu_id")
    @ResponseBody
    public String del_exam_user_by_eu_id(int eu_id) {
        int i = examUserService.del_exam_user_by_eu_id(eu_id);
        if (i == 1) {
            responseResult.setCode(200);
            responseResult.setMessage("删除成功");
        } else {
            responseResult.setCode(0);
            responseResult.setMessage("删除失败");
        }
        return JsonUtils.getJson(responseResult);
    }

    @RequestMapping("/review_exam")
    public String review_exam(int eu_id, Model model) {
        model.addAttribute("eu_id", eu_id);
        return "views/manage/review_exam";
    }


    @RequestMapping("/get_exam_result_by_eu_id")
    @ResponseBody
    public String get_exam_result_by_eu_id(int eu_id) {
        ExamUser examResultByEuId = examUserService.get_exam_result_by_eu_id(eu_id);

        Map<String, Object> map = new HashMap<>();
        map.put("exam_res", examResultByEuId);
        String ep_scq_c_ids = examResultByEuId.getExam().getExamPaper().getEp_scq_c_ids();
        String[] scq_c_ids = ep_scq_c_ids.split(",");
        List<Object> scq_data = new ArrayList<>();
        for (String c_id : scq_c_ids) {
            if (!c_id.equals("")) {
                SingleChoiceQuestions scqByCId = singleChoiceQService.get_scq_by_c_id(Integer.parseInt(c_id));
                if (scqByCId == null) {
                    continue;
                }
                scq_data.add(scqByCId);
            }
        }
        map.put("scq_data", scq_data);

        String ep_jq_j_ids = examResultByEuId.getExam().getExamPaper().getEp_jq_j_ids();
        String[] jq_j_ids = ep_jq_j_ids.split(",");
        List<Object> jq_data = new ArrayList<>();
        for (String j_id : jq_j_ids) {
            if (!j_id.equals("")) {
                JudgmentQuestions jqByJId = judgmentQService.get_jq_by_j_id(Integer.parseInt(j_id));
                if (jqByJId == null) {
                    continue;
                }
                jq_data.add(jqByJId);
            }
        }
        map.put("jq_data", jq_data);

        String ep_saq_sa_ids = examResultByEuId.getExam().getExamPaper().getEp_saq_sa_ids();
        String[] saq_sa_ids = ep_saq_sa_ids.split(",");
        List<Object> saq_data = new ArrayList<>();
        for (String sa_id : saq_sa_ids) {
            if (!sa_id.equals("")) {
                ShortAnswerQuestions saqBySaId = shortAnswerQService.get_saq_by_sa_id(Integer.parseInt(sa_id));
                if (saqBySaId == null) {
                    continue;
                }
                saq_data.add(saqBySaId);
            }
        }
        map.put("saq_data", saq_data);


        String ep_fbq_fb_ids = examResultByEuId.getExam().getExamPaper().getEp_fbq_fb_ids();
        String[] fbq_fb_ids = ep_fbq_fb_ids.split(",");
        List<Object> fbq_data = new ArrayList<>();
        for (String fb_id : fbq_fb_ids) {
            if (!fb_id.equals("")) {
                FillBlanksQuestions fbqByFbId = fillBlanksQService.get_fbq_by_fb_id(Integer.parseInt(fb_id));
                if (fbqByFbId == null) {
                    continue;
                }
                fbq_data.add(fbqByFbId);
            }
        }
        map.put("fbq_data", fbq_data);

        //处理简答题评分
        Map<String, Object> u_saq_score_map = new HashMap<>();
        String[] pr_saq_res = examResultByEuId.getPaperResult().getPr_saq_res().split("\\*\\*\\*");
        for (String pr_saq : pr_saq_res) {
            if (!pr_saq.equals("")) {
                String[] pr_saqs = pr_saq.split("_");
                ShortAnswerQuestions saqBySaId = shortAnswerQService.get_saq_by_sa_id(Integer.parseInt(pr_saqs[0]));
                if (saqBySaId == null) {
                    continue;
                }
                String saq_answer = saqBySaId.getSa_answer();
                String u_saq_answer = pr_saqs[1];
                float similarityRatio = SimilarityUtils.getSimilarityRatio(saq_answer, u_saq_answer, true);
                float sa_score;
                if (similarityRatio > 0.4) {
                    sa_score = similarityRatio * examResultByEuId.getExam().getE_saq_score();
                } else {
                    sa_score = 0;
                }
                System.out.println("----------------------->" + sa_score);
                u_saq_score_map.put(pr_saqs[0], sa_score);
            }
        }
        map.put("u_saq_score_map", u_saq_score_map);


        //处理填空题评分
        Map<String, Object> u_fbq_score_map = new HashMap<>();
        String[] pr_fbq_res = examResultByEuId.getPaperResult().getPr_fbq_res().split(",");
        for (String pr_fbq : pr_fbq_res) {
            if (!pr_fbq.equals("")) {
                String[] pr_fbqs = pr_fbq.split("_");
                FillBlanksQuestions fbqByFbId = fillBlanksQService.get_fbq_by_fb_id(Integer.parseInt(pr_fbqs[0]));
                if (fbqByFbId == null) {
                    continue;
                }
                String fbq_answers = fbqByFbId.getFb_answer();
                String u_fbq_answers = pr_fbqs[1];
                String[] fbq_answer = fbq_answers.split(",");
                String[] u_fbq_answer = u_fbq_answers.split("\\*\\*\\*");
                int right_num = 0;
                for (int i = 0; i < fbq_answer.length; i++) {
                    float similarityRatio = SimilarityUtils.getSimilarityRatio(fbq_answer[i], u_fbq_answer[i], true);
                    if (similarityRatio < 0.8) {
                        continue;
                    }
                    right_num += 1;
                }

                DecimalFormat df=new DecimalFormat("0.0");//设置保留位数
                float fb_score= ((float)right_num/(fbq_answer.length))*examResultByEuId.getExam().getE_fbq_score();
//                System.out.println("----------------------->" + right_num);
//                System.out.println("----------------------->" + df.format(fb_score));
                u_fbq_score_map.put(pr_fbqs[0], df.format(fb_score));
            }
        }
        map.put("u_fbq_score_map", u_fbq_score_map);


        responseResult.setCode(200);
        responseResult.setMessage("成功");
        responseResult.setResult(map);
        System.out.println(JsonUtils.getJson(responseResult));
        return JsonUtils.getJson(responseResult);
    }

    @RequestMapping("/browse_exam_res")
    public String browse_exam_res(int eu_id, Model model) {
        model.addAttribute("eu_id", eu_id);
        return "views/user/browse_exam";
    }

    @RequestMapping("/admin_browse_exam_res")
    public String admin_browse_exam_res(int eu_id, Model model) {
        model.addAttribute("eu_id", eu_id);
        return "views/manage/browse_exam";
    }


    @RequestMapping("/get_data_statistics_by_e_id")
    @ResponseBody
    public String get_data_statistics_by_e_id(int e_id) {

        //获取考试人数
        int exam_total = examUserService.statistic1(e_id);
        //获取未完成人数
        int no_finish_total = examUserService.statistic2(e_id);
        //获取完成人数
        int finish_total = exam_total - no_finish_total;
        int no_review_total = 0;
        int review_total = 0;
        int pass_total = 0;
        //试卷总分
        int score_total = examUserService.statistic5(e_id);
        //及格分数
        int score_pass = examUserService.statistic7(e_id);

        List<ExamUser> examUsers = null;
        if (finish_total != 0) {
            //获取未批阅人数
            no_review_total = examUserService.statistic3(e_id);
            //获取已批阅人数
            review_total = finish_total - no_review_total;
            if (review_total != 0) {
                //获取及格人数
                pass_total = examUserService.statistic4(e_id);
                //获取排名
                examUsers = examUserService.statistic6(e_id);
            }
        }

        Map<String, Object> map = new HashMap<>();
        map.put("exam_total", exam_total);
        map.put("no_finish_total", no_finish_total);
        map.put("finish_total", finish_total);
        map.put("no_review_total", no_review_total);
        map.put("review_total", review_total);
        map.put("pass_total", pass_total);
        map.put("score_total", score_total);
        map.put("score_pass", score_pass);
        map.put("examUsers", examUsers);
        responseResult.setCode(200);
        responseResult.setMessage("成功");
        responseResult.setResult(map);
        return JsonUtils.getJson(responseResult);
    }


    @RequestMapping("/get_score_by_u_id")
    @ResponseBody
    public String get_score_by_u_id(int u_id) {
        List<ExamUser> scoreByUId = examUserService.get_score_by_u_id(u_id);
        responseResult.setCode(200);
        responseResult.setMessage("成功");
        responseResult.setMessage("成功");
        responseResult.setResult(scoreByUId);
        return JsonUtils.getJson(responseResult);
    }


}

